/**
 * FileName: ServerStringEncoder
 * Author:   powersi
 * Date:     2021/3/1 20:04
 * Description:
 */
package com.demo.zm.netty.sample.echo.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.string.StringEncoder;

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/1 20:04
 */
@ChannelHandler.Sharable
public class ServerStringEncoder extends MessageToMessageEncoder<String> {

    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
        if (msg.length() == 0) {
            return;
        }
        ByteBuf byteBuf = ctx.alloc().buffer();
        byteBuf.writeInt(1);
        byteBuf.writeBytes(msg.getBytes());
        out.add(byteBuf);
    }
}
